﻿using EfCore.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Text;

namespace EfCore.Data.Models.Mapping
{
    public class ProvinceMap
        : IEntityTypeConfiguration<Province>
    {
        public void Configure(EntityTypeBuilder<Province> builder)
        {
            //Table.
            builder.ToTable("T_Province");
            //Key.
            builder.HasKey(t => t.Id);
            //Props.
            builder.Property(t => t.Id)
                .HasColumnName("Id")
                .IsRequired(true);
            builder.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(30)
                .IsUnicode(true)
                .IsRequired(true);
            builder.Property(t => t.Population)
                .HasColumnName("Population")
                .IsRequired(true);
            //Relations.
            builder.HasMany(t => t.Cities)
                .WithOne(t => t.Province)
                .HasForeignKey(t => t.ProvinceId);
            //Index.
            builder.HasIndex(t => t.Name);
            //Seed data.
            //var province = new Province()
            //{
            //    Id = 1,
            //    Name = "安徽省",
            //    Population = 100_000_000,
            //};
            //province.Cities.Add(new City()
            //{
            //    ProvinceId = 1,
            //    Name = "合肥市",
            //    AreaCode = "00010001",
            //    CityCompanies = new List<CityCompany>()
            //    {
            //         new CityCompany(){ Company=new Company{
            //              Name="凤凰新华",
            //         },
            //         City=new City(){
                          
            //         } }
            //    }
            //});
            //builder.HasData(province);
        }
    }
}
